package com.example.com.skills_utf8;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

import com.utils.TIME;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by matengfei on 15/8/10.
 */
public class _64CircleProgress extends View {

    private String TAG = _64CircleProgress.class.getSimpleName();


    Paint paint = new Paint();
    Paint paint_arc = new Paint();
    Paint paint_bitmap = new Paint();

    int radius = 200;
    int stroke_width = 25;
    RectF circle_rect = new RectF();

    //Timer timer = new Timer();
    Handler handler = new Handler();
    int progress = 0;


    Bitmap bitmap_pause;
    Bitmap bitmap_start;
    Bitmap bitmap_background;


    int status = States.pause;
    boolean bComplete = false;
    boolean bLoop = false;

    Listener listener;


    // 自定义图案
    BitmapShader bitmapshader_diy;
    Paint paint_diy ;



    boolean bFirstClick = false;
    boolean bClickPure = false;


    int mProgress = 0;
    int mProgressMax = 0;


    public _64CircleProgress(Context context, AttributeSet attrs) {
        super(context, attrs);


        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(stroke_width);
        paint.setAntiAlias(true);

        paint_arc.setColor(Color.YELLOW);
        paint_arc.setStyle(Paint.Style.STROKE);
        paint_arc.setStrokeWidth(stroke_width);
        paint_arc.setAntiAlias(true);



        // handler.postDelayed(new MyRunnable(), 1000);



        // 加载暂停，播放按钮
        //BitmapDrawable bitmapdrawable_pause = (BitmapDrawable)getResources().getDrawable(R.drawable.pause_circle, context.getTheme());
        //bitmap_pause = bitmapdrawable_pause.getBitmap();

        bitmap_pause = BitmapFactory.decodeResource(getResources(), R.drawable.pause_circle);
        bitmap_start = BitmapFactory.decodeResource(getResources(), R.drawable.start_circle);

        //bitmap_background = BitmapFactory.decodeResource(getResources(), R.drawable.circle_background);


        // 使用图案填充
        //bitmapshader_diy = new BitmapShader(bitmap_pause, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        //paint_diy.setShader(bitmapshader_diy);


        // 背景呈现不规则向外扩散，使用粒子效果的原理即可，使用灰度图与颜色进行alpha混合；
        // 在播放上有个淡入淡出效果



    }


    // 内存不足时保存成员变量
    @NonNull
    @Override
    public Parcelable onSaveInstanceState() {
        return super.onSaveInstanceState();
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        super.onRestoreInstanceState(state);
    }


    @Override
    protected synchronized void onDraw(Canvas canvas) {
       //super.onDraw(canvas);


        progress = getProgress();

        if(!bLoop){
            if(bComplete){
                return;
            }
        }

        if(progress >= getMax()){
            if(listener!=null){
                listener.OnComplete();
                bComplete = true;
            }
            progress = 0;
        }



        /*
        if(bClickPure){

            int left = getWidth()/2-radius-20;
            int top = getHeight()/2-radius-20;

            // 绘制背景
            canvas.drawBitmap(bitmap_background,
                    left,top,
                    paint_bitmap);

            // 设置动画


            bClickPure = false;
        }
        */


        circle_rect.set(getWidth() / 2 - radius, getHeight() / 2 - radius, getWidth() / 2 + radius, getHeight() / 2 + radius);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);

        canvas.drawArc(circle_rect, -90, progress, false, paint_arc);

        if(status == States.start){
            canvas.drawBitmap(bitmap_pause,
                    circle_rect.centerX()-bitmap_pause.getWidth()/2,
                    circle_rect.centerY()-bitmap_pause.getHeight()/2,
                    paint_bitmap);
        }
        else{
            canvas.drawBitmap(bitmap_start,
                    circle_rect.centerX() - bitmap_pause.getWidth() / 2,
                    circle_rect.centerY() - bitmap_pause.getHeight() / 2,
                    paint_bitmap);
        }

        //Log.e(TAG, "progress: " +progress );



    }


    public void setProgress(int progress){
        mProgress = progress;
    }

    public int getProgress(){
        return mProgress;
    }

    public void setMax(int progress_max){
        mProgressMax = progress_max;
    }
    public int getMax(){
        return mProgressMax;
    }



    public interface Listener{
        public void OnComplete();
    }




    public void click(){
        if(status == States.pause){
            status = States.start;

            if(!bFirstClick){
                bFirstClick = true;
                bClickPure = true;
            }

        }
        else{
            status = States.pause;
        }


        invalidate();
    }



    class States{
        public static final int pause = 0;
        public static final int start = 1;
        public static final int stop = 2;

    }





}
